package com.etc.zmall.service.impl;

import java.util.List;

import com.etc.dao.ShopCartDao;
import com.etc.dao.impl.ShopCartDaoImpl;
import com.etc.entity.Shop;
import com.etc.entity.Shopc;
import com.etc.util.DBUtil;
import com.etc.util.PageData;
import com.etc.zmall.service.ShopCartService;

/**
 * 
 * <p>
 * Title: ShopCartServiceImpl
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年1月11日
 * 
 */
public class ShopCartServiceImpl implements ShopCartService {
	ShopCartDao sc = new ShopCartDaoImpl();

	@Override
	public PageData<Shopc> showShopCar(int userid, int page, int pageSize, String keywords) {
		if (page < 1) {
			page = 1;
		} else if (page > pageSize) {
			page = pageSize;
		}
		PageData<Shopc> pd = sc.selShopCar(userid, page, pageSize, keywords);
		if (pd == null)
			return null;
		return pd;
	}

	@Override
	public boolean delGoods(int userid, String goodsname) {
		boolean flag = sc.delShopCar(userid, goodsname);
		if (flag)
			return true;
		return false;
	}

	@Override
	public boolean addGoods(Shopc shopc) {
		boolean flag = false;
		// 业务逻辑判断
		// 如果购物车表中已经存在当前商品的信息，则完成更新功能即可.(改变数量)
		Shopc result = sc.getShopcByUserIdAndGoodsId(shopc.getUserid(), shopc.getGoodsid(), shopc.getShopid());
		System.out.println("result :"+result);
		if (result == null) {
			// 如果购物车表中不存在当前商品的信息，则完成insert.
			flag = sc.addShopCar(shopc);
		} else {
			//将查询出来的result中的shopcid赋值给shopc对象
			shopc.setShopcid(result.getShopcid());
			flag = sc.updateShopcByshopcid(shopc);
		}
		return flag;
	}

	@Override
	public Shopc showShopName(Shopc shopc) {
		String sql = "select shopname from tbl_shop where shopid=?";
		List<Shop> list = (List<Shop>) DBUtil.exQuery(sql, Shop.class, shopc.getShopid());
		if (list.size() == 0) {
			return null;
		}
		Shopc shopc1 = new Shopc(shopc.getUserid(), shopc.getShopid(), shopc.getGoodsname(), shopc.getShopccount(),
				shopc.getTotal(), list.get(0).getShopname());
		System.out.println(shopc1.getShopname());
		return shopc1;
	}
}
